package parser;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import database.OBUdata;

import objects.OBUentry;

public class Parser {
	
	public static void main (String[] args) throws FileNotFoundException{
		Parser test1 = new Parser("src/testdata/run1.txt");
		test1.processLine();
                Parser test2 = new Parser("src/testdata/run2.txt");
                test2.processLine();
	}
	
	private File file;
	private OBUdata obudb;

	public Parser(String fileName){
		try {
			obudb = new OBUdata();
		}
		catch (java.lang.Exception exc){
			System.out.println("Count not connect to DB");	
			System.exit(1);
		}
		file = new File(fileName);
	}
	
	@SuppressWarnings("deprecation")
	public void processLine() throws FileNotFoundException{

                try{
                        FileInputStream fileInput = new FileInputStream(file);

                        BufferedInputStream buffer = new BufferedInputStream(fileInput);
                        DataInputStream dataInput = new DataInputStream(buffer);

                        while (dataInput.available() !=0){
                                convertLine(dataInput.readLine());
                        }
                        fileInput.close();
                        buffer.close();
                        dataInput.close();
                }catch  (FileNotFoundException e){
                        e.printStackTrace();
                }catch (IOException e){
                        e.printStackTrace();
                }finally {
			obudb.close();
		}
	}
	
	//Function converts each line processed into the correct format & puts into database
	public void convertLine(String newLine){
		if (!newLine.contains("&")){
		
			Scanner scanner = new Scanner(newLine);
			scanner.useDelimiter(",");
			if (scanner.hasNext()){
				
				//Test to see if the GPS data is RMC format.  If not skips the line.
				String formatTest = scanner.next();
				if (formatTest.equals("$GPRMC")){
					Double latDeg = 0.0, latMin = 0.0, lonDeg = 0.0, lonMin = 0.0;
					String latDegS = "", latMinS = "", lonDegS = "", lonMinS = "";
					
					//Read unused information
					String trash = scanner.next();
					trash = scanner.next();
				
					//Convert latitude degrees and minutes to double
					String latitude = scanner.next();
						for (int y = 0; y < 2; y++){
							latDegS += latitude.charAt(y);
						}
						latDeg = Double.valueOf(latDegS);
						for (int x = 2; x < 9; x++){
							latMinS += latitude.charAt(x);
						}
						latMin = Double.valueOf(latMinS);
						latMin /= 60;
				
					//Read more unused information
					trash = scanner.next();
					
					//Convert longitude degrees and minutes to double
					String longitude = scanner.next();
						for (int y = 0; y < 3; y++){
							lonDegS += longitude.charAt(y);
						}
						lonDeg = Double.valueOf(lonDegS);
						for (int x = 3; x < 10; x++){
							lonMinS += longitude.charAt(x);
						}
					lonMin = Double.valueOf(lonMinS);
					lonMin /= 60;
				
					//Read more unused information
					trash = scanner.next();
					
					//Convert knots to double
					Double knots = Double.valueOf(scanner.next());
					
					//Read more unused information
					trash = scanner.next();
					trash = scanner.next();
					trash = scanner.next();
					trash = scanner.next();
				
					double newLatitude = latDeg + latMin;
					double newLongitude = lonDeg + lonMin;
					newLongitude = 0-newLongitude;
				
					//Convert knots to integer
					int newKnots = (int)(knots * 1.15077945);
				
					OBUentry obu1 = new OBUentry();
					obu1.setLat(newLatitude);
					obu1.setLong(newLongitude);
					obu1.setSeatBelt(true);
					obu1.setSpeed(newKnots);

					try {
						obudb.addOBUentry(obu1);
					}
					catch (java.sql.SQLException sexcept){
						System.out.println("Coult not add to database " + sexcept.getMessage());
					}
	
					//System.out.println(newLatitude);
					//System.out.println(newLongitude);
					//System.out.println(newKnots);
				}
			}
			scanner.close();
		}
	}
}
